package com.tank;



import java.awt.*;
import java.awt.Graphics;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 继承Frame,Graphics 画出一个坦克样子
 */
public class TankFrame extends Frame {

    int x=100,y=100;
    /**
     * 坦克对象
     */
    Tank myTank = new Tank(x, y, Dir.DOWN,this);
    /**
     * 子弹
     */
    List<Bullet> bullets = new ArrayList();


    static final int GAME_WIDTH = 800;
    static final int GAME_HEIGHT = 600;
    Image offScreenImg = null;

    public TankFrame(){
        setSize(GAME_WIDTH,GAME_HEIGHT);
        setResizable(false);
        setTitle("Tank War");
        setVisible(true);

        //按键监听
        addKeyListener(new TankKeyListener());
        // 窗口监听
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("closing");
                System.exit(0);
            }
        });
    }
    @Override
    public void update(Graphics g){
        if(null == offScreenImg){
            offScreenImg = this.createImage(GAME_WIDTH,GAME_HEIGHT);
        }
        Graphics gOffScreen= offScreenImg.getGraphics();
        Color color = gOffScreen.getColor();
        gOffScreen.setColor(Color.BLACK);
        gOffScreen.fillRect(0,0,GAME_WIDTH,GAME_HEIGHT);
        gOffScreen.setColor(color);
        paint(gOffScreen);
        g.drawImage(offScreenImg,0,0,null);
    }

    @Override
    public void paint(Graphics graphics){
        myTank.paint(graphics);
        for(int i=0; i<bullets.size(); i++){
            bullets.get(i).paint(graphics);
        }
    }

    class TankKeyListener extends KeyAdapter {

        boolean bU = false;
        boolean bL = false;
        boolean bR = false;
        boolean bD = false;

        @Override
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            switch (key){
                case KeyEvent.VK_LEFT:
                    bL =true;
                    break;
                case  KeyEvent.VK_RIGHT:
                    bR =true;
                    break;
                case KeyEvent.VK_UP:
                    bU =true;
                    break;
                case  KeyEvent.VK_DOWN:
                    bD =true;
                    break;
            }
            setTankFrameDir();
        }

        @Override
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();
            switch (key){
                case KeyEvent.VK_LEFT:
                    bL =false;
                    break;
                case  KeyEvent.VK_RIGHT:
                    bR =false;
                    break;
                case KeyEvent.VK_UP:
                    bU =false;
                    break;
                case  KeyEvent.VK_DOWN:
                    bD = false;
                    break;
                case KeyEvent.VK_SPACE:
                    myTank.fire();
                    break;
                default:break;
            }
            setTankFrameDir();
        }

        private void setTankFrameDir(){
            if(!bL && !bR && !bU && !bD ){
                myTank.setMoving(false);
            }else {
                myTank.setMoving(true);
                if(bL) myTank.setDir(Dir.LEFT);
                if(bR) myTank.setDir(Dir.RIGHT);
                if(bU) myTank.setDir(Dir.UP);
                if(bD) myTank.setDir(Dir.DOWN);
            }
        }

    }
}
